import { api } from '../boot/axios';
import { Group, GroupSetting, GroupSettingOptions, Permission, StockItemGroup, TaskGroup } from './type';

function url(s: string) {
  if (s == '') {
    return ['/api/groupSettings', ''].join('/');
  }
  return ['/api/groupSettings', s.replace('/', ''), ''].join('/');
}

export async function listGroupSetting(groupname: string) {
  const res = await api({
    url: url('filter'),
    method: 'post',
    data: {
      groupname: groupname,
    },
  });
  return res.data as GroupSetting[];
}

export async function getGroupSetting(id: number) {
  const res = await api({
    url: url('' + id),
    method: 'get',
  });
  return res.data as GroupSetting;
}

export async function patchGroupSetting(
  id: number,
  data: Partial<GroupSetting> = {}
) {
  const res = await api({
    url: url('' + id),
    method: 'patch',
    data,
  });
  return res.data as GroupSetting;
}

export async function createGroupSetting(data: Partial<GroupSetting> = {}) {
  const res = await api({
    url: url(''),
    method: 'post',
    data,
  });
  return res.data as GroupSetting;
}

export async function deleteGroupSetting(id: number, username: string) {
  const res = await api({
    url: url('delete_group_setting'),
    method: 'delete',
    data: {
      id,
      username,
    },
  });
  return res.data as GroupSetting;
}

export async function bulkUpdateGroupSetting(data: GroupSetting[]) {
  const res = await api({
    url: url('bulk_update_group_settings'),
    method: 'post',
    data: {
      data,
    },
  });
  return res.data as GroupSetting;
}

export async function getGroupSettingOptions(groupname: string) {
  const res = await api({
    url: url('get_setting_options'),
    method: 'post',
    data: {
      groupname: groupname,
    },
  });
  return res.data as GroupSettingOptions;
}

export async function listPermission() {
  const res = await api({
    url: url('list_permission'),
    method: 'get',
  });
  return res.data.data as Permission[];
}

export async function createGroupAndPermissionAndTask(
  group: string,
  canDoTask: boolean,
  canDoSC: boolean,
  tasks: string[] | null,
  stockCodes: string[] | null
) {
  const res = await api({
    url: url('create_group_and_permission_and_task'),
    method: 'post',
    data: {
      group: group,
      can_do_task: canDoTask,
      can_do_sc: canDoSC,
      tasks: tasks,
      stockCodes:stockCodes
    },
  });
  return res.data.data as Permission[];
}

export async function updateGroupAndPermissionAndTask(
  group_id: number,
  canDoTask: boolean,
  canDoSC: boolean,
  groupname: string,
  tasks: string[] | null,
  stockCodes: string[] | null
  ) {
  const res = await api({
    url: url('patch_group_and_permission'),
    method: 'patch',
    data: {
      group_id: group_id,
      can_do_task: canDoTask,
      can_do_sc: canDoSC,
      groupname: groupname,
      tasks: tasks,
      stockCodes:stockCodes
    },
  });
  return res.data.data as Permission[];
}

export async function listGroup() {
  const res = await api({
    url: url('list_group'),
    method: 'get',
  });
  return res.data.data as Group[];
}

export async function deleteGroup(id: number) {
  const res = await api({
    url: url('delete_group'),
    method: 'delete',
    data: {
      id,
    },
  });
  return res.data.data as Group[];
}

export async function getGroup(id: number) {
  const res = await api({
    url: url('get_group'),
    method: 'post',
    data: {
      id,
    },
  });
  return res.data;
}

export async function deleteGroupPermission(id: number, group_id: number) {
  const res = await api({
    url: url('delete_group_permission'),
    method: 'delete',
    data: {
      id,
      group_id,
    },
  });
  return res.data.data as Group[];
}

export async function deleteTaskGroup(id: number) {
  const res = await api({
    url: url('delete_task_group'),
    method: 'delete',
    data: {
      id,
    },
  });
  return res.data.data as Group[];
}

export async function getTaskGroup(id: number) {
  const res = await api({
    url: url('get_task_group'),
    method: 'post',
    data: {
      id,
    },
  });
  return res.data as TaskGroup[];
}


export async function getStockItemGroup(id: number) {
  const res = await api({
    url: url('get_stock_item_group'),
    method: 'post',
    data: {
      id,
    },
  });
  return res.data as StockItemGroup[];
}

export async function deleteStockGroup(id: number) {
  const res = await api({
    url: url('delete_stock_group'),
    method: 'delete',
    data: {
      id,
    },
  });
  return res.data.data as Group[];
}